<?php

	session_start();
	require("connect.php");
	$link = connect_APO();
	
	require("check.php");
	is_logged_in();
	
	$midweekName=$_GET['midweek_name'];
	
	$drive = $_POST['drive'];
	$phoneSelf = $_POST['phoneSelf'];
	$meID = $_SESSION['ID'];
	$type = $_SESSION['type'];
	$userID = $_POST['userID'];
	$phoneUser = $_POST['phoneUser'];
	
	$dontadd = checkPhone($phoneSelf,$midweekName);
	
	if($type == 'leader' || $type == 'coordinator' && $phoneUser != null) {
		$dontadd = checkPhone($phoneUser,$midweekName);
	}
	
	$phone = $phoneSelf;
	
	if($userID != null){
		$meID = $userID;
		$phone = $phoneUser;
	}
	
	require("header+logoutB.php");
	headerL();
	
	if(strcmp($drive,"yes")==0){
		$driveInt=1;
	} else {
		$driveInt=0;
	}
	
	if($dontadd!=1){
		if(strcmp($phone,"")!=0){
			echo "in here";
			$sql="UPDATE `users` SET `PhoneNumber`='$phone' WHERE `users`.`ID`='$meID'";
			$result=mysql_query($sql);
		}
		
		$sql="INSERT INTO `$midweekName` (`Name`, `ID`, `Drive`) VALUES ('$midweekName', '$meID', '$driveInt')";
		
		if(!mysql_query($sql)){
			die('Error: ' .mysql_error());
		}
		echo "<html>
		<link rel='stylesheet' type='text/css' href='webstyle.css' />
		<head>
			<title>Added to Midweek</title>
			<meta http-equiv='refresh' content='2; URL=midweekpage.php?midweek_name=$midweekName'>
		</head>
		<body>
		<center>
		<font class='mainFont'>"; if($meID == $userID){
			echo $userID." has ";
		}else {
			echo "You have ";
		}
		echo "been added to the midweek. You will be redirected back to the midweek in 2 seconds.</font>
		</center>
		</body>
		</html>";
	}

	require("footer.php");
	footer();	
	mysql_close($link);
?>